﻿Ext.define('YZSoft$Boot.login.Panel', {
    extend: 'Ext.Container',
    config: {
        loginRequestConfig: null,
        padding: '0 16 10 16',
        selectServer: true,
        serverUrl: null
    },

    constructor: function (config) {
        var me = this,
            strservers, servers = [], uid, server;

        me.banner = Ext.create('Ext.Component', {
            cls: 'logo-cnt',
            html: '<div class="logo"></div>'
        });

        if (localStorage) {
            try {
                strservers = localStorage.getItem('servers');
                Ext.Array.each(strservers ? Ext.decode(strservers) : [], function (server) {
                    if (server.url)
                        servers.push(server);
                });
                uid = localStorage.getItem('uid');
            }
            catch (exp) {
                servers = [];
                uid = '';
            }
        }

        server = (servers && servers[0]) || { url: '' };

        me.server = Ext.create('YZSoft$Boot.login.ServerSelect', {
            cls: 'server',
            label: '&#59719;',
            labelWidth: '32px',
            clearIcon: false,
            placeHolder: '请输入服务器地址',
            hidden: config.selectServer === false,
            store: {
                fields: ['url'],
                data: servers
            },
            value: server.url
        });

        me.uid = Ext.create('Ext.field.Text', {
            cls: 'uid',
            label: '&#59721;',
            labelWidth: '32px',
            clearIcon: true,
            placeHolder: $rs.LoginEmptyAccount,
            value: uid
        });

        me.pwd = Ext.create('Ext.field.Password', {
            cls: 'pwd',
            label: '&#59720;',
            labelWidth: '32px',
            clearIcon: true,
            placeHolder: $rs.LoginEmptyPassword
        });

        me.btnLogin = Ext.create('Ext.Button', {
            margin: '30 0 0 0',
            cls: ['yz-button-login'],
            text: $rs.LoginBtnLogin,
            disabled: false,
            handler: function (btn, e) {
                e.stopEvent();
                if (!me.server.isHidden()) {
                    var serverUrl = me.server.getValue();

                    if (!serverUrl)
                        return;
                }

                me.login();
            }
        });

        var cfg = {
            cls: ['yz-container-login'],
            scrollable: {
                direction: 'vertical',
                indicators: false
            },
            layout: {
                type: 'vbox',
                align: 'stretch'
            },
            items: [me.banner, me.server, me.uid, me.pwd, me.btnLogin, { flex: 1}]
        };

        Ext.apply(cfg, config);
        me.callParent([cfg]);
    },

    login: function () {
        if (navigator.connection) {
            if (navigator.connection.type == Connection.NONE) {
                alert($rs.NoConnection);
                return;
            }
        }

        var me = this,
            selectServer = me.getSelectServer() !== false,
            serverUrl = me.getServerUrl(),
            loginRequestConfig = me.getLoginRequestConfig(),
            url = serverUrl || (me.server.getValue() || '' || "https://bpm.cedarhd.com/emip").trim(),
            uid = (me.uid.getValue() || '').trim(),
            pwd = me.pwd.getValue(),
            ch, urllow;

       // serverUrl = ;



        if ((selectServer && !url) || !uid)
            return;

        if (selectServer || serverUrl) {
            urllow = url.toLowerCase();
            if (urllow.indexOf('http://') !== 0 && urllow.indexOf('https://') !== 0)
                url = 'http://' + url;

            ch = url.charAt(url.length - 1);
            if (ch == '/' || ch == '\\')
                url = url.substr(0, url.length - 1);

            Ext.Loader.setPath({
                'EMIP': url + '/EMIP'
            });
        }

        YZLoader.Ajax.request(Ext.apply({
            timeout: 8000,
            url: YZLoader.$url('YZSoft.Services/core/Auth.ashx'),
            waitMsg: { message: $rs.Authenticating, autoClose: false },
            delay: false,
            params: {
                Method: 'Login',
                uid: uid,
                pwd: pwd
            },
            success: function (action) {
                me.saveLoginInfo(url, uid, pwd);

                if (me.config.fn)
                    me.config.fn.call(me.scope, action.result, url);
            },
            failure: function (action) {
                Ext.Msg.show({
                    title: '登录失败',
                    message: action.result.errorMessage,
                    hideOnMaskTap: true,
                    buttons: [{
                        text: '我知道了',
                        flex: 1,
                        cls: 'yz-button-flat yz-button-action-hot',
                        itemId: 'ok'
                    }]
                });
            }
        }, loginRequestConfig));
    },

    saveLoginInfo: function (url, uid, pwd) {
        var me = this,
            newServers = [];

        try {
            newServers.push({
                url: url
            });

            servers = localStorage.getItem('servers');
            servers = servers ? Ext.decode(servers) : [];
            Ext.Array.each(servers, function (server) {
                if (!String.Equ(server.url, url))
                    newServers.push(server);
            });

            localStorage.setItem('servers', Ext.encode(newServers));
            localStorage.setItem('uid', uid);
            localStorage.setItem('pwd', pwd);
            localStorage.setItem('logout', 'false');
        }
        catch (exp) {
        }
    }
});